/**
 * Created by heshufen on 20/6/27.
 */
import React, {PureComponent} from 'react';
import {CachedImage} from 'react-native-img-cache';
import {Image} from 'react-native';
import {disposeImageUrl} from '../utils/imageUtils';

export default class CustomizeImage extends PureComponent {

    render() {
        const {style, quality, resizeModeValue = 1, originalImage = false, ...other} = this.props;

        //如果style是个数组，将它处理成一个对象
        let mergeStyle = {};
        const scan = (style) => {
            if (style instanceof Array) {
                style.map(item => {
                    scan(item);
                });
            } else if (style instanceof Object) {
                Object.assign(mergeStyle, style);
            }
        };
        scan(style);
        // alert(JSON.stringify(mergeStyle));


        if (isNetImage(other)) {
            if (!originalImage) {
                other.source.uri = disposeImageUrl(other.source.uri, mergeStyle, quality, resizeModeValue);
            }
            // alert("imageUrl:" + other.source.uri);
            return (
                <CachedImage style={mergeStyle} {...other}/>
            );
        } else {
            return <Image style={mergeStyle} {...other}/>;
        }
    }
}

function isNetImage(other) {
    if (other.source && other.source.uri && (other.source.uri.startsWith('https://') || other.source.uri.startsWith('http://'))) {
        return true;
    }
    return false;
}
